﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;
using Db4objects.Db4o.Config;

namespace ADB_QuanLyThuVien
{
    public class DAO_Member
    {
        public static bool AddEmployee(Member nv)
        {
            bool kq = true;
            if (isExist(nv) == true)
                kq = false;
            else
            {
                IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
                nv.Id = GetEmployeetId();
                db.Store(nv);
                db.Close();
            }
            return kq;
        }

        private static string GetEmployeetId()
        {
            string nextid = "NV";
            DateTime now = DateTime.Now;
            nextid += now.Year.ToString("0000") + now.Month.ToString("00") + now.Day.ToString("00") + now.Hour.ToString("00") + now.Minute.ToString("00") + now.Second.ToString("00");
            return nextid;
        }

        private static bool isExist(Member nv)
        {
            bool kq = true;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> list = db.Query(delegate(Member e) // câu truy vấn theo nagative query
            {
                if (e.Id.Contains("NV") && e.Name == nv.Name && e.Birthday == nv.Birthday && e.Address == nv.Address && e.PhoneNumber == nv.PhoneNumber && e.Email == nv.Email)
                    return true;
                return false;
            }
            );
            if (list.Count == 0)
                kq = false;
            db.Close();
            return kq;
        }

        public static List<Member> SelectAllEmployee()
        {
            List<Member> list = new List<Member>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member au) // câu truy vấn theo nagative query
            {
                if (au.Id.Contains("NV"))
                    return true;
                return false;
            }
             );
            for (int i = 0; i < result.Count; ++i)
                list.Add((Member)result[i]);
            db.Close();
            return list;
        }

        public static bool RemoveMember(string email)
        {
            bool kq = true;
            IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
            config.Common.ObjectClass(typeof(Member)).CascadeOnDelete(true);
            IObjectContainer db = Db4oEmbedded.OpenFile(config, QLTV_System.ConnectString);
            Member mem = new Member();
            mem.Email = email;
            IObjectSet result = db.QueryByExample(mem);
            if (result.Count > 0)
                db.Delete((Member)result[0]);
            else
                kq = false;
            db.Close();
            return kq;
        }

        internal static bool isEmail(string email)
        {
            bool kq = true;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            Member mem = new Member();
            mem.Email = email;
            IObjectSet result = db.QueryByExample(mem);
            if (result.Count == 0)
                kq = false;
            db.Close();
            return kq;
        }

        internal static List<Account> SellectAccEmployee()
        {
            List<Account> list = new List<Account>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            Author tg = new Author();
            IList<Member> result = db.Query(delegate(Member au) // câu truy vấn theo nagative query
            {
                if (au.Id.Contains("NV"))
                    return true;
                return false;
            }
             );
            for (int i = 0; i < result.Count; ++i)
            {
                Member mem = (Member)result[i];
                list.Add(mem.Acc);
            }
            db.Close();
            return list;
        }

        internal static List<Member> SelectAllReader()
        {
            List<Member> list = new List<Member>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member au) // câu truy vấn theo nagative query
            {
                if (au.Id.Contains("DG"))
                    return true;
                return false;
            }
             );
            for (int i = 0; i < result.Count; ++i)
                list.Add((Member)result[i]);
            db.Close();
            return list;
        }

        internal static bool AddReader(Member dg)
        {
            bool kq = true;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            dg.Id = GetReaderId();
            db.Store(dg);
            db.Close();
            return kq;
        }

        private static string GetReaderId()
        {
            string nextid = "DG";
            DateTime now = DateTime.Now;
            nextid += now.Year.ToString("0000") + now.Month.ToString("00") + now.Day.ToString("00") + now.Hour.ToString("00") + now.Minute.ToString("00") + now.Second.ToString("00");
            return nextid;
        }

        internal static List<Account> SellectAccReader()
        {
            List<Account> list = new List<Account>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member au) // câu truy vấn theo nagative query
            {
                if (au.Id.Contains("DG"))
                    return true;
                return false;
            }
            );
            for (int i = 0; i < result.Count; ++i)
            {
                Member mem = (Member)result[i];
                list.Add(mem.Acc);
            }
            db.Close();
            return list;
        }

        internal static bool isEmployee(Account acc)
        {
            bool kq = true;
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member mem) // câu truy vấn theo nagative query
            {
                if (mem.Acc.Username == acc.Username && mem.Id.Contains("NV"))
                    return true;
                return false;
            }
            );
            if (result.Count == 0)
                kq = false;
            db.Close();
            return kq;

        }

        internal static Member SelectMember(Account acc)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member mem) // câu truy vấn theo nagative query
            {
                if (mem.Acc.Username.ToUpper() == acc.Username.ToUpper())
                    return true;
                return false;
            }
            );
            Member user = (Member)result[0];
            db.Close();
            return user;
        }

        internal static Member SelectMemberByName(string nameReader)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member mem) // câu truy vấn theo nagative query
            {
                if (mem.Name == nameReader)
                    return true;
                return false;
            }
            );
            Member user =  new Member();
            if (result.Count > 0)
                user = (Member)result[0];
            db.Close();
            return user;
        }

        internal static Member SelectMemberByEmail(string email)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            Member mem = new Member();
            mem.Email = email;
            IObjectSet result = db.QueryByExample(mem);
            Member mmem = (Member)result.Next();
            db.Close();
            return mmem;
        }

        internal static List<Member> SelectMemberByID(string id)
        {
            List<Member> list = new List<Member>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member mem) // câu truy vấn theo nagative query
            {
                if (mem.Id.ToUpper().Contains(id.ToUpper()) && mem.Id.Contains("DG"))
                    return true;
                return false;
            }
            );
            for (int i = 0; i < result.Count; ++i)
                list.Add((Member)result[i]);
            db.Close();
            return list;
        }

        internal static List<Member> SelecMemberContainName(string name)
        {
            List<Member> list = new List<Member>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member mem) // câu truy vấn theo nagative query
            {
                if (mem.Name.ToUpper().Contains(name.ToUpper()) && mem.Id.Contains("DG"))
                    return true;
                return false;
            }
            );
            for (int i = 0; i < result.Count; ++i)
                list.Add((Member)result[i]);
            db.Close();
            return list;
        }

        internal static List<Member> SelectMemberContainEmail(string email)
        {
            List<Member> list = new List<Member>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member mem) // câu truy vấn theo nagative query
            {
                if (mem.Email.ToUpper().Contains(email.ToUpper()) && mem.Id.Contains("DG"))
                    return true;
                return false;
            }
            );
            for (int i = 0; i < result.Count; ++i)
                list.Add((Member)result[i]);
            db.Close();
            return list;
        }
    
        internal static List<Member> SelectMemberContainPhoneNumber(string sdt)
        {
            List<Member> list = new List<Member>();
            IObjectContainer db = Db4oEmbedded.OpenFile(QLTV_System.ConnectString);
            IList<Member> result = db.Query(delegate(Member mem) // câu truy vấn theo nagative query
            {
                if (mem.PhoneNumber.ToUpper().Contains(sdt.ToUpper()) && mem.Id.Contains("DG"))
                    return true;
                return false;
            }
            );
            for (int i = 0; i < result.Count; ++i)
                list.Add((Member)result[i]);
            db.Close();
            return list;
        }
    }
}